// pages/goEvaluate/goEvaluate.js
const app = getApp()
var firstNum = 0;
var secondNum = 0;
var thirdNum = 0;
var fourthNum = 0;
var fifthNum = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    firstStar: '/images/star.png',
    secondStar: '/images/star.png',
    thirdStar: '/images/star.png',
    fourthStar: '/images/star.png',
    fifthStar: '/images/star.png',
    iconImage: '/images/star.png',
    addImage: '/images/photoShop.png',
    cancelImage: '/images/evaluate_cancel.png',
    imageArr: [],
    inputCount: 0,
    otherContent: '',
    // 评分
    score: 5,
    // 订单id
    soiRid: 0,
    // 房间id
    sriRid: 0,
    isCanCommite: false,
    src: '',
    addT: '添加照片',
    // 判断是从详情还是订单页面进来的
    orderNum: 0, 
  },

  // 调用相机或相册选择照片
  gotoShow: function() {
    var that = this;
    wx.chooseImage({
      // 最多可以选择的图片张数，默认9
      count: 1,
      // original 原图，compressed 压缩图，默认二者都有
      sizeType: ['original', 'compressed'],
      // album 从相册选图，camera 使用相机，默认二者都有
      sourceType: ['album', 'camera'],
      success: function(res) {
        var tempFilePaths = res.tempFilePaths;  
        console.log(res)
        wx.showLoading({
          title: '',
        })
        wx.uploadFile({
          url: app.baseNet + 'file/ups',
          filePath: tempFilePaths[0],
          name: 'files',
          success: function (res) {
            wx.hideLoading()
            var data = JSON.parse(res.data);  
            that.data.imageArr.push(data[0].src)
            if (that.data.src == '') {
              that.data.src += data[0].src
            } else {
              that.data.src += ',' + data[0].src
            }
            that.setData({
              imageArr: that.data.imageArr,
              src: that.data.src,
              addT: that.data.imageArr.length + '/3',
            })
          }
        })
      },
    })
  },
  // 点击确认提交按钮
  entrueCommitBtnClick: function() {
    var that = this;
    var phoneNumber = wx.getStorageSync('phoneNumber')
    if (that.data.inputCount > 0) {
      wx.showLoading({
        title: '加载中...',
      })
      wx.request({
        url: app.baseNet + 'roomEvaluate/insertRoomEvaluate?',
        data: {
          username: phoneNumber,
          soiRid: that.data.soiRid,
          sriRid: that.data.sriRid,
          content: that.data.otherContent,
          src: that.data.src,
          score: that.data.score,
        },
        method: 'GET',
        header: {
          'Content-Type': 'application/json'
        },
        success(res) {
          console.log(res.data)
          wx.hideLoading()
          if (res.data.messcode == '001') {
            wx.showToast({
              title: '提交成功',
              duration: 500
            })
            setTimeout(function () {
              if (that.data.orderNum == 1) {
                // 从详情页进来的
                wx.navigateBack({
                  delta: 2
                })
              } else {
                // 从订单页面进来的
                wx.navigateBack({
                  delta: 1
                })
              }
            }, 1000)
          } else if (res.data.messcode == '002') {
            // 没有数据
          } else {
            // 服务器异常
          }
        },
      })
    } else {

    }
  },
  // 监听textArea输入的变化
  valueChange: function (e) {
    var that = this;
    that.setData({
      inputCount: e.detail.value.length,
      otherContent: e.detail.value,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      iconImage: options.iconImage,
      sriRid: options.sriRid,
      soiRid: options.soiRid,
      orderNum: options.orderNum,
    })
  },
  firstBtnClick: function() {
    var that = this;
    secondNum = 0;
    thirdNum = 0;
    fourthNum = 0;
    fifthNum = 0;
    if (firstNum % 2 == 0) {
      that.setData({
        firstStar: '/images/star.png',
        secondStar: '/images/grayStar.png',
        thirdStar: '/images/grayStar.png',
        fourthStar: '/images/grayStar.png',
        fifthStar: '/images/grayStar.png',
        score: 1,
      })
    } else {
      that.setData({
        firstStar: '/images/grayStar.png',
        secondStar: '/images/grayStar.png',
        thirdStar: '/images/grayStar.png',
        fourthStar: '/images/grayStar.png',
        fifthStar: '/images/grayStar.png',
        score: 0,
      })
    }
    firstNum += 1
  },
  secondBtnClick: function () {
    var that = this;
    firstNum = 0;
    thirdNum = 0;
    fourthNum = 0;
    fifthNum = 0;
    if (secondNum % 2 == 0) {
      that.setData({
        firstStar: '/images/star.png',
        secondStar: '/images/star.png',
        thirdStar: '/images/grayStar.png',
        fourthStar: '/images/grayStar.png',
        fifthStar: '/images/grayStar.png',
        score: 2,
      })
    } else {
      that.setData({
        firstStar: '/images/star.png',
        secondStar: '/images/grayStar.png',
        thirdStar: '/images/grayStar.png',
        fourthStar: '/images/grayStar.png',
        fifthStar: '/images/grayStar.png',
        score: 1,
      })
    }
    secondNum += 1
  },
  thirdBtnClick: function () {
    var that = this;
    firstNum = 0;
    secondNum = 0;
    fourthNum = 0;
    fifthNum = 0;
    if (thirdNum % 2 == 0) {
      that.setData({
        firstStar: '/images/star.png',
        secondStar: '/images/star.png',
        thirdStar: '/images/star.png',
        fourthStar: '/images/grayStar.png',
        fifthStar: '/images/grayStar.png',
        score: 3,
      })
    } else {
      that.setData({
        firstStar: '/images/star.png',
        secondStar: '/images/star.png',
        thirdStar: '/images/grayStar.png',
        fourthStar: '/images/grayStar.png',
        fifthStar: '/images/grayStar.png',
        score: 2,
      })
    }
    thirdNum += 1
  },
  fourthBtnClick: function () {
    var that = this;
    firstNum = 0;
    secondNum = 0;
    thirdNum = 0;
    fifthNum = 0;
    if (fourthNum % 2 == 0) {
      that.setData({
        firstStar: '/images/star.png',
        secondStar: '/images/star.png',
        thirdStar: '/images/star.png',
        fourthStar: '/images/star.png',
        fifthStar: '/images/grayStar.png',
        score: 4,
      })
    } else {
      that.setData({
        firstStar: '/images/star.png',
        secondStar: '/images/star.png',
        thirdStar: '/images/star.png',
        fourthStar: '/images/grayStar.png',
        fifthStar: '/images/grayStar.png',
        score: 3,
      })
    }
    fourthNum += 1
  },
  fifthBtnClick: function () {
    var that = this;
    firstNum = 0;
    secondNum = 0;
    thirdNum = 0;
    fourthNum = 0;
    if (fifthNum % 2 == 0) {
      that.setData({
        firstStar: '/images/star.png',
        secondStar: '/images/star.png',
        thirdStar: '/images/star.png',
        fourthStar: '/images/star.png',
        fifthStar: '/images/star.png',
        score: 5,
      })
    } else {
      that.setData({
        firstStar: '/images/star.png',
        secondStar: '/images/star.png',
        thirdStar: '/images/star.png',
        fourthStar: '/images/star.png',
        fifthStar: '/images/grayStar.png',
        score: 4,
      })
    }
    fifthNum += 1
  },
  // 删除按钮的点击事件
  cancelImageClick: function(e) {
    console.log(e)
    var that = this
    var tag = parseInt(e.currentTarget.dataset.id)
    var arr = that.data.imageArr
    arr.splice(tag, 1)
    if (arr.length == 0) {
      that.setData({
        imageArr: arr,
        addT: '添加照片',
      })
    } else {
      that.setData({
        imageArr: arr,
        addT: arr.length + '/3',
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})